package com.smart.smartdata;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.smart.smartdata.ble.SPO2BleManager;
import com.smart.smartdata.ble.TemperatureBleManager;
import com.smart.smartdata.eventbus.EventBus;
import com.smart.smartdata.eventbus.EventNames;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SettingActivity extends AppCompatActivity  implements EventBus.SubscriberChangeListener {
    @Bind(R.id.iv_back)
    ImageView iv_back;
    @Bind(R.id.iv_spo2)
    ImageView iv_spo2;
    @Bind(R.id.iv_temperature)
    ImageView iv_temperature;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        StatusBarUtil.setRootViewFitsSystemWindows(this, false);
        StatusBarUtil.setTranslucentStatus(this);//透明状态栏
        ButterKnife.bind(this);

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        iv_spo2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("type","spo2");
                AppUtils.jump(SettingActivity.this,ScanBleActivity.class,bundle);
            }
        });
        iv_temperature.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("type","temperature");
                AppUtils.jump(SettingActivity.this,ScanBleActivity.class,bundle);
            }
        });

        if(SPO2BleManager.getInstance().getBleDevice()==null){
            iv_spo2.setBackground(getResources().getDrawable(R.mipmap.icon_spo2_normal));
        }else{
            iv_spo2.setBackground(getResources().getDrawable(R.mipmap.icon_spo2_selected));
        }
        if(TemperatureBleManager.getInstance().getBleDevice()==null){
            iv_temperature.setBackground(getResources().getDrawable(R.mipmap.icon_temperature_normal));
        }else{
            iv_temperature.setBackground(getResources().getDrawable(R.mipmap.icon_temperature_selected));
        }
    }

    @Override
    public void onSubscriberDataChanged(Object notificationName, Object content) {
        if (EventNames.CONNECT_SUCCESS == notificationName) {
            if (content != null) {
                String type = (String) content;
                if ("spo2".equals(type)) {
                    iv_spo2.setBackground(getResources().getDrawable(R.mipmap.icon_spo2_selected));
                } else if ("temperature".equals(type)) {
                    iv_temperature.setBackground(getResources().getDrawable(R.mipmap.icon_temperature_selected));
                }
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        EventBus.getInstance().subscribe(EventNames.CONNECT_SUCCESS, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getInstance().unSubscribe(EventNames.CONNECT_SUCCESS, this);
    }

}
